package cn.iocoder.yudao.module.eval.controller.admin.plan;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.plan.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.plan.EvalPlanDO;
import cn.iocoder.yudao.module.eval.service.plan.EvalPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 评价计划")
@RestController
@RequestMapping("/eval/plan")
@Validated
@Slf4j
public class EvalPlanController {

    @Resource
    private EvalPlanService planService;

    @PostMapping("/create")
    @Operation(summary = "创建评价计划")
    @PreAuthorize("@ss.hasPermission('eval:plan:create')")
    public CommonResult<Long> createPlan(@RequestBody EvalPlanSaveReqVO createReqVO) {
        log.info("Controller接收到创建计划请求: {}", createReqVO);
        return success(planService.createPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评价计划")
    @PreAuthorize("@ss.hasPermission('eval:plan:update')")
    public CommonResult<Boolean> updatePlan(@Valid @RequestBody EvalPlanSaveReqVO updateReqVO) {
        planService.updatePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评价计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:plan:delete')")
    public CommonResult<Boolean> deletePlan(@RequestParam("id") Long id) {
        planService.deletePlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评价计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:plan:query')")
    public CommonResult<EvalPlanRespVO> getPlan(@RequestParam("id") Long id) {
        EvalPlanDO plan = planService.getPlan(id);
        return success(BeanUtils.toBean(plan, EvalPlanRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得评价计划列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('eval:plan:query')")
    public CommonResult<List<EvalPlanRespVO>> getPlanList(@RequestParam("ids") Collection<Long> ids) {
        List<EvalPlanDO> list = planService.getPlanList(ids);
        return success(BeanUtils.toBean(list, EvalPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评价计划分页")
    @PreAuthorize("@ss.hasPermission('eval:plan:query')")
    public CommonResult<PageResult<EvalPlanRespVO>> getPlanPage(@Valid EvalPlanPageReqVO pageReqVO) {
        PageResult<EvalPlanDO> pageResult = planService.getPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评价计划 Excel")
    @PreAuthorize("@ss.hasPermission('eval:plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlanExcel(@Valid EvalPlanPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalPlanDO> list = planService.getPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "评价计划.xls", "数据", EvalPlanRespVO.class,
                BeanUtils.toBean(list, EvalPlanRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新计划状态")
    @PreAuthorize("@ss.hasPermission('eval:plan:update')")
    public CommonResult<Boolean> updatePlanStatus(@Valid @RequestBody EvalPlanStatusUpdateReqVO updateReqVO) {
        planService.updatePlanStatus(updateReqVO.getId(), updateReqVO.getPlanStatus());
        return success(true);
    }

    @PostMapping("/copy")
    @Operation(summary = "复制评价计划")
    @PreAuthorize("@ss.hasPermission('eval:plan:create')")
    public CommonResult<Long> copyPlan(@Valid @RequestBody EvalPlanCopyReqVO copyReqVO) {
        return success(planService.copyPlan(copyReqVO.getId(), copyReqVO.getNewPlanName(), copyReqVO.getNewPlanCode()));
    }

    @PostMapping("/batch-create")
    @Operation(summary = "批量创建评价计划")
    @PreAuthorize("@ss.hasPermission('eval:plan:create')")
    public CommonResult<List<Long>> batchCreatePlans(@Valid @RequestBody EvalPlanBatchCreateReqVO batchCreateReqVO) {
        return success(planService.batchCreatePlans(batchCreateReqVO, batchCreateReqVO.getBatchCount()));
    }

    @GetMapping("/list-by-status")
    @Operation(summary = "根据状态获得评价计划列表")
    @Parameter(name = "planStatus", description = "计划状态", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:plan:query')")
    public CommonResult<List<EvalPlanRespVO>> getPlanListByStatus(@RequestParam("planStatus") Integer planStatus) {
        List<EvalPlanDO> list = planService.getPlanListByStatus(planStatus);
        return success(BeanUtils.toBean(list, EvalPlanRespVO.class));
    }

    @PutMapping("/submit-approval")
    @Operation(summary = "提交计划审批")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:plan:submit')")
    public CommonResult<Boolean> submitPlanForApproval(@RequestParam("id") Long id) {
        planService.submitPlanForApproval(id);
        return success(true);
    }

    @PutMapping("/approve")
    @Operation(summary = "审批计划")
    @PreAuthorize("@ss.hasPermission('eval:plan:approve')")
    public CommonResult<Boolean> approvePlan(@Valid @RequestBody EvalPlanApproveReqVO approveReqVO) {
        planService.approvePlan(approveReqVO.getId(), approveReqVO.getApproved(), approveReqVO.getComments());
        return success(true);
    }

    @PutMapping("/publish")
    @Operation(summary = "发布计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:plan:publish')")
    public CommonResult<Boolean> publishPlan(@RequestParam("id") Long id) {
        planService.publishPlan(id);
        return success(true);
    }

    @PutMapping("/start")
    @Operation(summary = "开始评价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:plan:start')")
    public CommonResult<Boolean> startEvaluation(@RequestParam("id") Long id) {
        planService.startEvaluation(id);
        return success(true);
    }

    @PutMapping("/complete")
    @Operation(summary = "完成评价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:plan:complete')")
    public CommonResult<Boolean> completeEvaluation(@RequestParam("id") Long id) {
        planService.completeEvaluation(id);
        return success(true);
    }

    @PutMapping("/cancel")
    @Operation(summary = "取消计划")
    @PreAuthorize("@ss.hasPermission('eval:plan:cancel')")
    public CommonResult<Boolean> cancelPlan(@Valid @RequestBody EvalPlanCancelReqVO cancelReqVO) {
        planService.cancelPlan(cancelReqVO.getId(), cancelReqVO.getReason());
        return success(true);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取计划统计信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:plan:query')")
    public CommonResult<Object> getPlanStatistics(@RequestParam("id") Long id) {
        return success(planService.getPlanStatistics(id));
    }

    @GetMapping("/can-delete")
    @Operation(summary = "检查计划是否可以删除")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:plan:query')")
    public CommonResult<Boolean> canDeletePlan(@RequestParam("id") Long id) {
        return success(planService.canDeletePlan(id));
    }

    @GetMapping("/can-update")
    @Operation(summary = "检查计划是否可以修改")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:plan:query')")
    public CommonResult<Boolean> canUpdatePlan(@RequestParam("id") Long id) {
        return success(planService.canUpdatePlan(id));
    }

}
